package com.pbd.connection;

import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.SQLQuery;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.hibernate.persister.entity.AbstractEntityPersister;


public class HibernateUtil {

	public HibernateUtil() {
		super();
	}

	public static final SessionFactory sessionFactory;

	static {
		try {
			System.out.println("in HibernateUtil");
			
//			URL url = Thread.currentThread().getContextClassLoader().getResource("com/pbd/connection/hibernate.cfg.xml"); 
			Configuration cfg = new Configuration();
			
//			cfg.setProperty("hibernate.dialect","org.hibernate.dialect.MySQLDialect");
//			cfg.setProperty("hibernate.connection.url","jdbc:mysql://localhost/pbd");
//			cfg.setProperty("hibernate.connection.username", "root");
//			cfg.setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver");
//			cfg.setProperty("hibernate.connection.password", "root123");
//			cfg.setProperty("hibernate.transaction.factory_class","org.hibernate.transaction.JDBCTransactionFactory");
//			cfg.setProperty("hibernate.current_session_context_class","thread");
//			cfg.setProperty("hibernate.show_sql", "true");
//			cfg.addPackage("com.pbd.shared");
//			cfg.addAnnotatedClass(com.pbd.shared.PARTYCONTACT.class);
//			cfg.addAnnotatedClass(com.pbd.shared.PARTY.class);
//			cfg.addAnnotatedClass(com.pbd.shared.LEADMASTER.class);
//			cfg.addAnnotatedClass(com.pbd.shared.CORRESPONDENCEDETAIL.class);
//			cfg.addAnnotatedClass(com.pbd.shared.BUSINESSCATEGORY.class);
//			cfg.addAnnotatedClass(com.pbd.shared.BUSINESSCATEGORYRELATION.class);
//			cfg.addAnnotatedClass(com.pbd.shared.ALBUMMASTER.class);
//			cfg.addAnnotatedClass(com.pbd.shared.MEDIARELATION.class);
//			cfg.addAnnotatedClass(com.pbd.shared.AREA.class);
//			
			sessionFactory = cfg.configure().buildSessionFactory();
		} catch (Throwable ex) {
			ex.printStackTrace();
			throw new ExceptionInInitializerError(ex);
		}
	}
	int a;
	float f1;

	
	public static void main(String args[]) {
		sessionFactory.getAllClassMetadata();
	}

	
	private static void populateHiValue1(Map classMetaDataMap) {
		Session deleteSession = sessionFactory.openSession();
		Transaction deleteTtransaction = deleteSession.beginTransaction();
		try {
			SQLQuery deletequery = deleteSession.createSQLQuery(" delete from HIValueGenerator");
			deletequery.executeUpdate();
			deleteTtransaction.commit();
		} finally {
			deleteSession.close();
		}
		Set keySet = classMetaDataMap.keySet();
		if (keySet != null) {
			Iterator iter = keySet.iterator();
			while (iter.hasNext()) {
				String className = (String) iter.next();
				AbstractEntityPersister abstractEntityPersister = (AbstractEntityPersister) sessionFactory.getClassMetadata(className);
				String[] ideStrings = abstractEntityPersister.getIdentifierColumnNames();
				if (ideStrings != null) {
					int length = ideStrings.length;
					String identifierColumnName = ideStrings[0];
					String tableName = abstractEntityPersister.getTableName();
					Session session = sessionFactory.openSession();
					Transaction transaction = session.beginTransaction();
					try {
						String queryStr = " SELECT MAX(" + identifierColumnName + ") from " + tableName;
						System.out.println(" queryStr [" + queryStr);
						SQLQuery sQuery = session.createSQLQuery(queryStr);
						List dataList = sQuery.list();
						if (dataList != null) {
							Object value = dataList.get(0);
							if (value == null)
								value = -1;
							className = className.substring(className.lastIndexOf(".") + 1);
							if (!className.equalsIgnoreCase("PARTYLOGIN")) {
								String insertQuery = " insert into HIValueGenerator (NEXT_VAL_FILTER,NEXT_HI) VALUES ( '" + className + "' ," + value + ")";
								System.out.println(" insertQuery [" + insertQuery);
								SQLQuery insertquery = session.createSQLQuery(insertQuery);
								insertquery.executeUpdate();
							}

						}
						transaction.commit();
					} catch (Exception ex) {
						transaction.rollback();
						ex.printStackTrace();
						throw new RuntimeException(ex);
					} finally {
						session.close();
					}
				}
			}
		}
	}
}